package org.karelun.gef.view;

import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.ViewPart;
import org.karelun.gef.editors.IUmlEditorPart;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.parts.PartFactory;

public class ShowPartShow extends ViewPart {

	public ShowPartShow() {
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent,SWT.NONE);
		composite.setLayout(new GridLayout());
		GridData layoutData = new GridData(SWT.FILL,SWT.FILL,true,true);
		composite.setLayoutData(layoutData);
		GraphicalViewer viewer = new ScrollingGraphicalViewer();
		viewer.createControl(composite);
		viewer.getControl().setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		initialGraphcailViewer(viewer);
	}

	private void initialGraphcailViewer(GraphicalViewer viewer) {
		ScalableFreeformRootEditPart rootEditPart = new ScalableFreeformRootEditPart();
		viewer.setRootEditPart(rootEditPart);
		viewer.setEditPartFactory(new PartFactory());
		
		viewer.setContents(getDiagram());
		
	}

	private DiagramModel getDiagram() {
		IWorkbenchPage page = getSite().getWorkbenchWindow().getActivePage();
		IEditorPart activeEditor = page.getActiveEditor();
		if(activeEditor instanceof IUmlEditorPart){
			IUmlEditorPart editor=(IUmlEditorPart)activeEditor;
			DiagramModel diagram = editor.getDiagram();
			return diagram;
		}
		return new DiagramModel();
	}

	@Override
	public void setFocus() {

	}

}
